package com.cotacao;

import java.util.ArrayList;
import java.util.List;

import com.controller.CotacaoController;
import com.controller.ProdutoController;
import com.controller.ProdutoMarcaController;
import com.dao.CotacaoDao;
import com.dao.EmpresaDao;
import com.dao.ProdutoDao;
import com.database.TransactionManager;
import com.model.CotacaoBean;
import com.model.EmpresaBean;
import com.model.ProdutoBean;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Preco extends Activity {

	private Intent i;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final TransactionManager transaction = new TransactionManager(getApplicationContext());
		transaction.start();
		setContentView(R.layout.activity_preco);
		Button btn = (Button) findViewById(R.id.voltar3);
		Button btn1 = (Button) findViewById(R.id.cadastrar3);
		i = new Intent(this, Cotacao.class);

		EmpresaDao empDao = new EmpresaDao();
		final List<EmpresaBean> empList = empDao.getAll(transaction);
        ProdutoDao prodDao = new ProdutoDao();
        final List<ProdutoBean> prodList = prodDao.getAll(transaction);
		
        spinnerI(empList,prodList);
		
		btn1.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
				long empresaId = spinner1.getSelectedItemId();
				
				Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
				long produtoId = spinner2.getSelectedItemId();
				
				EditText data = (EditText) findViewById(R.id.editData);
                String dtText = data.toString();
                EditText valor = (EditText) findViewById(R.id.voltar);
                Double real = Double.parseDouble(valor.toString());
				
				EmpresaBean emp = empList.get((int) empresaId);
				ProdutoBean prod = prodList.get((int) produtoId);
				
				CotacaoController cot = new CotacaoController();
				try {
					cot.inserir(transaction, prod.getId(), emp.getId(), dtText, "", real);
							
					transaction.commit();
					//					Toast.makeText(this, "Registro salvo com sucesso", Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Log.d("ERROR", e.getMessage());
					transaction.rollback();
				}finally{
					finish();				
				}
			}
		});

		btn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				transaction.rollback();
				finish();				
			}
		});
	}

	protected void spinnerI(List<EmpresaBean> list, List<ProdutoBean> list2) {
		ArrayList<String> result = new ArrayList<String>();
		for (EmpresaBean Bean : list) {
			result.add(Bean.getFantasia());
		}

		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, result);
		Spinner spinner = (Spinner) findViewById(R.id.spinner1) ;
		spinner.setAdapter(arrayAdapter);
		
		ArrayList<String> result2 = new ArrayList<String>();
		for (ProdutoBean Bean : list2) {
			result2.add(Bean.getDescricao());
		}

		ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, result2);
		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2) ;
		spinner2.setAdapter(arrayAdapter2);
		
	};
	protected void TableView(TransactionManager transaction) {
		CotacaoDao dao = new CotacaoDao();
		List<CotacaoBean> cotList = dao.getAll(transaction);

		ArrayList<String> list = new ArrayList<String>();
		for (CotacaoBean Bean : cotList) {
			list.add(Bean.getProd().getDescricao());
		}

		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
		ListView listView = (ListView) findViewById(R.id.listView4);
		listView.setAdapter(arrayAdapter);
		OnItemClickListener listener = new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adp, View v,	int pos, long id) {
//				Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT ).show();
			}
		};
		listView.setOnItemClickListener(listener);
	}
}